import redis
import time

redis_config = {
    "host": "127.0.0.1",   # host参数使用字符串
    "port": 6379,   # 端口号，默认6379
    "password": "",  # 密码
    # "db": 5 # 数据库的名字，5表示第6个数据库
}

#创建数据的连接池
pool = redis.ConnectionPool(**redis_config)
#通过连接池创建一个数据库连接
conn = redis.Redis(connection_pool=pool)

#操作String
# conn.set("name", "坚果")
# print(conn.get("name"))	# 获取到字节类型， byte，而不是字符串
# a = str(conn.get("name"), encoding='utf-8')    # encoding：转换字符的时候使用utf-8格式转换
# print(a)
# print(conn.strlen("name"))
# print(conn.getrange("name", 1, 4))  # 获取字符串并切片
# old_value = conn.getset("name", "坚果1")  # 设置指定key的新值，有旧值时返回，没有返回None
# print(old_value)
# print(str(old_value, encoding='utf-8'))
# new_value = str(conn.get("name"), encoding="utf"-8)
# print(new_value)
#
#
# # 操作List
# conn.lpush("mylist8848", "baili", "beifan", "weiwei")
# time.sleep(1)
# print(conn.lrange("mylist8848", 0, -1))
#
# #操作set
# conn.sadd("myset456","one","two","three")
# time.sleep(1)
# print(conn.smembers("myset456"))
#
# # 操作hash
# conn.hset("myhash666","k1","v1")
# time.sleep(1)
# print(conn.hget("myhash666","k1"))

# conn.close()# 关闭redis连接